package top.hcode.hoj.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import top.hcode.hoj.common.result.CommonResult;
import top.hcode.hoj.pojo.dto.BaseIdDTO;
import top.hcode.hoj.pojo.dto.coll.CollCreatedWorksJudgeDTO;
import top.hcode.hoj.pojo.dto.coll.QueryCollWorksDTO;
import top.hcode.hoj.pojo.dto.coll.QueryCollWorksStatDTO;
import top.hcode.hoj.pojo.entity.coll.CollCreatedWorks;
import top.hcode.hoj.pojo.vo.coll.CollCreatedWorksStatVO;
import top.hcode.hoj.pojo.vo.coll.CollCreatedWorksVO;
import top.hcode.hoj.service.UserAuthService;
import top.hcode.hoj.service.admin.AdminCollCreatedWorksService;

import java.util.List;

/**
 * @author orangej
 * @since 2025-01-21
 */
@Slf4j
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
@Api(tags = "作品管理")
public class AdminCollCreatedWorksController {

    private final AdminCollCreatedWorksService collCreatedWorksService;
    private final UserAuthService userAuthService;

    @ApiOperation(value = "获取作品列表")
    @ApiOperationSupport(order = 10)
    @GetMapping("/admin/coll-created-works/list")
    @RequiresAuthentication
    @RequiresRoles("teacher")
    public CommonResult<IPage<CollCreatedWorksVO>> getCollCreatedWorksList(QueryCollWorksDTO queryCollWorksDTO) {
        List<String> schoolIdList = userAuthService.getSchoolIdList();
        return CommonResult.successResponse(collCreatedWorksService.getCollCreatedWorksList(queryCollWorksDTO, schoolIdList));
    }


    @ApiOperation(value = "获取作品详情")
    @ApiOperationSupport(order = 20)
    @GetMapping("/admin/coll-created-works/detail")
    @RequiresAuthentication
    @RequiresRoles("teacher")
    public CommonResult<CollCreatedWorksVO> getCollCreatedWorks(BaseIdDTO dto) {
        return CommonResult.successResponse(collCreatedWorksService.getCollCreatedWorksDetail(String.valueOf(dto.getId())));
    }

    @ApiOperation(value = "获取作品统计信息")
    @ApiOperationSupport(order = 30)
    @GetMapping("/admin/coll-created-works/stat")
    @RequiresAuthentication
    @RequiresRoles("teacher")
    public CommonResult<CollCreatedWorksStatVO> getCollCreatedWorks(QueryCollWorksStatDTO queryWorksStatDTO) {
        List<String> schoolIdList = userAuthService.getSchoolIdList();
        return CommonResult.successResponse(collCreatedWorksService.getStatInfo(queryWorksStatDTO, schoolIdList));
    }

    @ApiOperation(value = "评分", notes = "最终得分为所有评分的平均分")
    @ApiOperationSupport(order = 40)
    @PostMapping("/admin/coll-created-works/judge")
    @RequiresAuthentication
    @RequiresRoles("teacher")
    public CommonResult<Void> saveCollCreatedWorks(@RequestBody CollCreatedWorksJudgeDTO judgeDTO) {
        String uid = userAuthService.uid();
        collCreatedWorksService.judge(judgeDTO.getWorksId(), judgeDTO.getScore(), uid);
        return CommonResult.successResponse();
    }

    @PostMapping("/admin/coll-created-works/save")
    @RequiresAuthentication
    @RequiresRoles("teacher")
    public CommonResult<CollCreatedWorks> saveCollCreatedWorks(@RequestBody CollCreatedWorks collCreatedWorks) {
        return CommonResult.successResponse(collCreatedWorksService.saveCollCreatedWorks(collCreatedWorks));
    }

    @PostMapping("/admin/coll-created-works/delete")
    @RequiresAuthentication
    @RequiresRoles("teacher")
    public CommonResult<Void> deleteCollCreatedWorks(@RequestBody BaseIdDTO dto) {
        collCreatedWorksService.deleteCollCreatedWorks(dto.getId());
        return CommonResult.successResponse();
    }

}
